#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/13
# @Author :songtx
# @File   :i2c.py


import time
import numpy as np
from smbus import SMBus
import pymysql

bus = SMBus(1)  # 创建接口

while 1:
    list1, list2 = [], []
    for i in range(5):  # 初始化，预读n组数据
        data1 = bus.read_word_data(0x40, 0x00)
        list1.append(data1)
        data2 = bus.read_word_data(0x41, 0x00)
        list2.append(data2)
        time.sleep(0.1)
    average1 = round(sum(list1) / len(list1))  # 平均值四舍五入
    average2 = round(sum(list2) / len(list2))
    i2c40 = average1
    i2c41 = average2
    con = pymysql.connect(
            host='127.0.0.1',
            port=3306,
            user='root',
            password='!qAz2WsX3edc',
            db='ph',
            charset='utf8'
    )
    cur = con.cursor()
    
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany(
                "INSERT i2c(time,i2c40,i2c41) VALUE(%s,%s,%s)",
                [(time1, i2c40, i2c41)])
    con.commit()
    time.sleep(0.8)
    con.commit()
        


